package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.business.domain.FileInternetExport;
import com.ruoyi.business.domain.FilePerson;
import com.ruoyi.business.domain.vo.FileInternetExportVo;
import com.ruoyi.business.mapper.FileInternetExportMapper;
import com.ruoyi.business.service.IFileInternetExportService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateExtUtils;
import com.ruoyi.common.utils.StringExtUtils;
import com.ruoyi.workflow.service.IProcessInstanceService;
import com.ruoyi.workflow.utils.WorkFlowUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 外网导出Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-13
 */
@Service
public class FileInternetExportServiceImpl implements IFileInternetExportService {
    @Autowired
    private FileInternetExportMapper fileInternetExportMapper;
    @Autowired
    private IProcessInstanceService iProcessInstanceService;

    /**
     * 查询外网导出
     *
     * @param id 外网导出ID
     * @return 外网导出
     */
    @Override
    public FileInternetExport selectFileInternetExportById(Long id) {
        FileInternetExport fileInternetExport = fileInternetExportMapper.selectFileInternetExportById(id);
        WorkFlowUtils.setStatusFileValue(fileInternetExport, fileInternetExport.getId().toString());
        return fileInternetExport;
    }

    /**
     * 查询外网导出列表
     *
     * @param fileInternetExport 外网导出
     * @return 外网导出
     */
    @Override
    public List<FileInternetExport> selectFileInternetExportList(FileInternetExport fileInternetExport) {
        return fileInternetExportMapper.selectFileInternetExportList(fileInternetExport);
    }

    @Override
    public TableDataInfo<FileInternetExportVo> selectPageFileInternetExportList(FileInternetExportVo fileIntranetExportVo, PageQuery pageQuery) {
        Page<FileInternetExportVo> result = fileInternetExportMapper.selectJoinPage(pageQuery.build(), FileInternetExportVo.class, this.buildQueryWrapper(fileIntranetExportVo));
        List<FileInternetExportVo> records = result.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<String> collectIds = records.stream().map(FileInternetExportVo::getId).collect(Collectors.toList());
            ArrayList<String> ids = new ArrayList<>();//因没有vo对象，只能转string
            WorkFlowUtils.setStatusListFileValue(records, collectIds, "id");
        }
        result.setRecords(records);
        return TableDataInfo.build(result);
    }

    private MPJLambdaWrapper<FileInternetExport> buildQueryWrapper(FileInternetExportVo fileInternetExport) {
        Map<String, Object> params = fileInternetExport.getParams();
        MPJLambdaWrapper<FileInternetExport> fileInternetExportJoinLambdaWrapper = new MPJLambdaWrapper<FileInternetExport>();
        fileInternetExportJoinLambdaWrapper
                .selectAll(FileInternetExport.class)
                .selectAs(SysUser::getNickName, FileInternetExport::getApplicantName)
                .selectAs(FilePerson::getName, FileInternetExport::getExportedPersonName)
                .selectAs(SysDept::getDeptName, FileInternetExport::getApplicantUnitName)
                .leftJoin(SysUser.class, SysUser::getUserId, FileInternetExport::getApplicant)
                .leftJoin(FilePerson.class, FilePerson::getId, FileInternetExport::getExportedPerson)
                .leftJoin(SysDept.class, SysDept::getDeptId, FileInternetExport::getApplicantUnit)
                .eq(BeanUtil.isNotEmpty(fileInternetExport.getApplicant()), FileInternetExport::getApplicant, fileInternetExport.getApplicant())
                .eq(BeanUtil.isNotEmpty(fileInternetExport.getApplicantUnit()), FileInternetExport::getApplicantUnit, fileInternetExport.getApplicantUnit())
                .eq(BeanUtil.isNotEmpty(fileInternetExport.getExportedPerson()), FileInternetExport::getExportedPerson, fileInternetExport.getExportedPerson())
                .between(params.containsKey("beginApplicantDate"), FileInternetExport::getApplicantDate, params.get("beginApplicantDate"), params.get("endApplicantDate"))
                .eq(BeanUtil.isNotEmpty(fileInternetExport.getStatus()), FileInternetExport::getStatus, fileInternetExport.getStatus())
                .eq(BeanUtil.isNotEmpty(fileInternetExport.getApprovalStatus()), FileInternetExport::getApprovalStatus, fileInternetExport.getApprovalStatus())
                .orderByDesc(FileInternetExport::getApplicantDate);

        return fileInternetExportJoinLambdaWrapper;
    }

    /**
     * 新增外网导出
     *
     * @param fileInternetExport 外网导出
     * @return 结果
     */
    @Override
    public FileInternetExport insertFileInternetExport(FileInternetExport fileInternetExport) {
        fileInternetExport.setCreateTime(DateExtUtils.getNowDate());
        fileInternetExport.setId(IdUtil.getSnowflakeNextId());
        fileInternetExportMapper.insertFileInternetExport(fileInternetExport);
        return fileInternetExport;
    }

    /**
     * 修改外网导出
     *
     * @param fileInternetExport 外网导出
     * @return 结果
     */
    @Override
    public FileInternetExport updateFileInternetExport(FileInternetExport fileInternetExport) {
        fileInternetExportMapper.updateFileInternetExport(fileInternetExport);
        fileInternetExport.setUpdateTime(DateExtUtils.getNowDate());
        return fileInternetExport;
    }

    /**
     * 批量删除外网导出
     *
     * @param ids 需要删除的外网导出ID
     * @return 结果
     */
    @Override
    public int deleteFileInternetExportByIds(Long[] ids) {
        return fileInternetExportMapper.deleteFileInternetExportByIds(ids);
    }

    /**
     * 删除外网导出信息
     *
     * @param id 外网导出ID
     * @return 结果
     */
    @Override
    public int deleteFileInternetExportById(Long id) {
        String processInstanceId = iProcessInstanceService.getProcessInstanceId(id.toString());
        if (StringExtUtils.isNotBlank(processInstanceId)) { //删除相关流程数据
            iProcessInstanceService.deleteRuntimeProcessAndHisInst(processInstanceId);

        }
        return fileInternetExportMapper.deleteFileInternetExportById(id);
    }
}
